<?php

$db = new WebSalesDB();
$badchars = array('<', '>', '€', '&');
$goodchars = array('&lt;', '&gt;', '&#8364;', '&amp;');

function getPriceRange($CatID, $TLCID) {
    global $badchars;
    global $goodchars;
    $List = array();
    $Range_Start = 0;
    $Range_End = 25;
    $Range_inc = 25;
    for ($i = 0; $i < 9000; $i+=$Range_inc) {
        $Prices_sql = "SELECT WebRPHIGH,COUNT(WebRPHIGH) AS Count FROM CostBiterStock WHERE $CatID = '$TLCID' AND WebRPHIGH >= $Range_Start AND WebRPHIGH < $Range_End";
        $Prices_q = DB::query($Prices_sql);
        while ($Prices = DB::fetch($Prices_q)) {
            if ($Prices['Count'] > 0) {
                $List = array_merge($List, array('&#8364;' . $Range_Start . ' - &#8364;' . $Range_End => $Prices['Count']));
            }
        }
        $Range_Start += $Range_inc;
        $Range_End += $Range_inc;
        if ($i + $Range_inc == 100) {
            $i = 100;
            $Range_inc = 50;
            $Range_Start = 100;
            $Range_End = 150;
        }
        if ($i + $Range_inc == 500) {
            $i = 500;
            $Range_inc = 100;
            $Range_Start = 500;
            $Range_End = 600;
        }
    }
    return $List;
}

function getBrandList($CatID, $TLCID) {
    global $badchars;
    global $goodchars;
    $List = array();
    $Makes_sql = "SELECT DISTINCT Manufacturer,COUNT(Manufacturer) AS Count FROM CostBiterStock WHERE $CatID = '$TLCID' GROUP BY Manufacturer";
    $Makes_q = DB::query($Makes_sql);
    while ($Makes = DB::fetch($Makes_q)) {
        $List = array_merge($List, array(str_replace($badchars, $goodchars, $Makes['Manufacturer']) => $Makes['Count']));
    }
    return $List;
}

$fp = fopen('../xml/menu.xml', 'w');
fwrite($fp, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<Categories>\n");

$TopLevelCategory = "SELECT DISTINCT MasterCat,MasterCatID FROM CostBiterStock WHERE MasterCat != ''";
$TLC_qry = DB::query($TopLevelCategory);

while ($TLC = DB::fetch($TLC_qry)) {
    $TLC['MasterCat'] = str_replace($badchars, $goodchars, $TLC['MasterCat']);
    fwrite($fp, "\t<Category Name='" . $TLC['MasterCat'] . "'>\n");

    /*     * * Price Category ** */
    $Prices = getPriceRange("MasterCatID", $TLC['MasterCatID']);
    fwrite($fp, "\t\t<Price>\n");
    foreach ($Prices as $Range => $Count) {
        fwrite($fp, "\t\t\t<Range Count='" . $Count . "'>" . $Range . "</Range>\n");
    }
    fwrite($fp, "\t\t</Price>\n");

    /*     * * Brand Category ** */
    $Brands = getBrandList("MasterCatID", $TLC['MasterCatID']);
    fwrite($fp, "\t\t<Brand>\n");
    foreach ($Brands as $Name => $Count) {
        fwrite($fp, "\t\t\t<Manufacturer Count='" . $Count . "'>" . $Name . "</Manufacturer>\n");
    }
    fwrite($fp, "\t\t</Brand>\n");


    fwrite($fp, "\t</Category>\n");
}

$TopLevelCategory = "SELECT DISTINCT WebCat,WebCatID FROM CostBiterStock WHERE WebCat != ''";
$TLC_qry = DB::query($TopLevelCategory);

while ($TLC = DB::fetch($TLC_qry)) {
    $TLC['WebCat'] = str_replace($badchars, $goodchars, $TLC['WebCat']);
    fwrite($fp, "\t<Category Name='" . $TLC['WebCat'] . "'>\n");

    /*     * * Price Category ** */
    $Prices = getPriceRange("WebCatID", $TLC['WebCatID']);
    fwrite($fp, "\t\t<Price>\n");
    foreach ($Prices as $Range => $Count) {
        fwrite($fp, "\t\t\t<Range Count='" . $Count . "'>" . $Range . "</Range>\n");
    }
    fwrite($fp, "\t\t</Price>\n");

    /*     * * Brand Category ** */
    $Brands = getBrandList("WebCatID", $TLC['WebCatID']);
    fwrite($fp, "\t\t<Brand>\n");
    foreach ($Brands as $Name => $Count) {
        fwrite($fp, "\t\t\t<Manufacturer Count='" . $Count . "'>" . $Name . "</Manufacturer>\n");
    }
    fwrite($fp, "\t\t</Brand>\n");
    fwrite($fp, "\t</Category>\n");
}

$TopLevelCategory = "SELECT DISTINCT SubCat,SubCatID FROM CostBiterStock WHERE SubCat != ''";
$TLC_qry = DB::query($TopLevelCategory);

while ($TLC = DB::fetch($TLC_qry)) {
    $TLC['SubCat'] = str_replace($badchars, $goodchars, $TLC['SubCat']);
    fwrite($fp, "\t<Category Name='" . $TLC['SubCat'] . "'>\n");

    /*     * * Price Category ** */
    $Prices = getPriceRange("SubCatID", $TLC['SubCatID']);
    fwrite($fp, "\t\t<Price>\n");
    foreach ($Prices as $Range => $Count) {
        fwrite($fp, "\t\t\t<Range Count='" . $Count . "'>" . $Range . "</Range>\n");
    }
    fwrite($fp, "\t\t</Price>\n");

    /*     * * Brand Category ** */
    $Brands = getBrandList("SubCatID", $TLC['SubCatID']);
    fwrite($fp, "\t\t<Brand>\n");
    foreach ($Brands as $Name => $Count) {
        fwrite($fp, "\t\t\t<Manufacturer Count='" . $Count . "'>" . $Name . "</Manufacturer>\n");
    }
    fwrite($fp, "\t\t</Brand>\n");
    fwrite($fp, "\t</Category>\n");
}

fwrite($fp, "</Categories>\n");
fclose($fp);
echo "File 'menu.xml' created";
?>